
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:go_router/go_router.dart';
import 'package:xim/common/get_style_view.dart';
import 'package:xim/common/style/colors.dart';
import 'package:xim/common/util/collection.dart';
import 'package:xim/common/util/datetime.dart';
import 'package:xim/common/util/system.dart';
import 'package:xim/http/apis/message/message_api.dart';
import 'package:xim/http/apis/message/message_model.dart';
import 'package:xim/http/ase.dart';
import 'package:xim/http/rsa.dart';
import 'package:xim/http/store.dart';
import 'package:xim/screen/message_list_screen/page/message_detail_info_page/message_detail_controller.dart';
import 'package:xim/screen/message_list_screen/page/message_detail_info_page/message_detail_style.dart';
import 'package:xim/widget/common/state.dart';

/// 消息详细消息页面
class ChatMessageDetailPage extends GetStyleControllerView<ChatMessageModel, MessageDetailController, MessageDetailStyle> {

  final int messageId;

  final showContentCount = 12;

  final secretTextController = TextEditingController();

  ChatMessageDetailPage(this.messageId,{super.key});

  @override
  Widget buildView(BuildContext context, ChatMessageModel? data) {
    ChatMessageModel chatMessage = data!;
    return Scaffold(
      appBar: style.appBar,
      backgroundColor: style.bgColor.color,
      body: Container(
        padding: const EdgeInsets.only(left: 12,right: 12,top: 12),
        child: Column(
          children: [
            Expanded(
              child: Column(
                children: [
                  style.titleRowItem("明文", Text(controller.plaintextContent)),
                  style.titleRowItem("密文", GestureDetector(
                    onTap: (){
                      secretTextController.text = chatMessage.content;
                    },
                    child: Text(
                      chatMessage.content
                          .sub(end: showContentCount)
                          .append("...",predicate: (str)=> str.length>showContentCount),
                      style: const TextStyle(color: StyleColor.textLinkColor),
                    ),
                  )),
                  style.titleRowItem("加密密钥", GestureDetector(
                    onTap: (){},
                    child: Text(
                      chatMessage.secretKey.secretKey
                          .sub(end: showContentCount)
                          .append("...",predicate: (str)=> str.length>showContentCount),
                      style: const TextStyle(color: StyleColor.textLinkColor),
                    ),
                  )),
                  style.titleRowItem("明文密钥", GestureDetector(
                    onTap: (){

                    },
                    child: Text(
                      controller.plaintextMsgKey
                          .sub(end: showContentCount)
                          .append("...",predicate: (str)=> str.length>showContentCount),
                      style: const TextStyle(color: StyleColor.textLinkColor),
                    ),
                  )),
                  style.textRowItem("密钥版本号", chatMessage.secretKey.secretKeyVersion.toString()),
                  style.textRowItem("密钥创建者", chatMessage.secretKey.creatorAccount),
                  style.textRowItem("密钥创建时间", chatMessage.secretKey.createTime.format("yyyy-MM-dd HH:mm:ss")),
                  const Divider(),
                  style.textRowItem("消息发送者", chatMessage.userInfo.account),
                  style.textRowItem("消息发送者名称", chatMessage.userInfo.name),
                  style.textRowItem("消息时间时间", chatMessage.sendTime.format("yyyy-MM-dd HH:mm:ss")),
                ].splitJoin(const SizedBox(height: 6,)).toList(),
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 6),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  GestureDetector(
                    onTap: (){
                      // TODO 零知识证明，不泄露密钥情况下，保证审核人员知道明文
                      // hash(明文) | messageId -> message ASE密文 & ASE加密后的密钥
                    },
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        border: Border.all(color: const Color(0xffebedf0)),
                        borderRadius: const BorderRadius.all(Radius.circular(4)),
                      ),
                      padding: const EdgeInsets.symmetric(horizontal: 26, vertical: 8),
                      child: const Center(child: Text("举报", style: TextStyle(color: Color(0xff323233),)),),
                    ),
                  ),
                  GestureDetector(
                    onTap: ()=>Get.back(),
                    child: Container(
                      decoration: const BoxDecoration(
                        color: Color(0xff07c160),
                        borderRadius: BorderRadius.all(Radius.circular(4)),
                      ),
                      padding: const EdgeInsets.symmetric(horizontal: 26, vertical: 8),
                      child: const Center(child: Text("取消", style: TextStyle(color: Colors.white),))
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }


}

